package com.qjdchina.sku.front.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.qjdchina.sku.model.ProductSetComponent;
import com.qjdchina.sku.model.vo.ProductSetCompareVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.commons.model.PageModel;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.ProductSetManager;
import com.qjdchina.sku.consts.WebConsts;
import com.qjdchina.sku.model.query.ProductSetQueryVO;
import com.qjdchina.sku.model.vo.ConfigurationVO;
import com.qjdchina.sku.model.vo.ProductSetVO;
import com.qjdchina.sku.service.enums.SkuResultCode;

/**
 * 产品
 * @author shenpingfeng
 * 2017年1月4日
 */
@Controller
@RequestMapping(value = "productSet")
public class ProductSetController {

	public static final Logger LOGGER = LoggerFactory.getLogger(ProductSetController.class);
	@Autowired
	private ProductSetManager productSetManager;
	

	/**
	 * 根据条件查询产品
	 * shenpingfeng
	 * 2017年1月4日
	 * @param productSetQueryVO
	 * @param strPage
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "query", method = RequestMethod.POST)
	@ResponseBody
	public RestBody<PageModel<ProductSetVO>> query(ProductSetQueryVO productSetQueryVO,
			@RequestParam(value = "pageNo", required = false) String strPage,@RequestParam(value = "pageSize", required = false) Integer pageSize, HttpServletRequest request) {		
	
		PageModel<ProductSetVO> pageModel = new PageModel<ProductSetVO>();
		int pageNo = 1;
		if (StringUtils.isNotEmpty(strPage) && StringUtils.isNumeric(strPage)) {
			pageNo = Integer.parseInt(strPage);
		}
		pageModel.setPageNo(pageNo);
		if(pageSize == null){
			pageSize = 15;
		}
		pageModel.setPageSize(pageSize);
		Result<PageModel<ProductSetVO>> result = productSetManager.queryPagedProductSetList(productSetQueryVO, pageModel);
		if (!result.isSuccess()) {
			return new RestBody<PageModel<ProductSetVO>>(result.getCode(), result.getMessage());
		}
		PageModel<ProductSetVO> pageSkuProduct = result.getDataModel();

		return new RestBody<PageModel<ProductSetVO>>(SkuResultCode.SUCCESS, pageSkuProduct);

	}
	/**
	 * 查询产品详情
	 * shenpingfeng
	 * 2017年1月4日
	 * @param productSetId
	 * @return
	 */
	@RequestMapping(value = "productSetDetail", method = RequestMethod.GET)
	@ResponseBody
	public RestBody<List<ConfigurationVO>> productSetDetail(Integer productSetId) {
		Result<List<ConfigurationVO>> result = productSetManager.queryProductSetDetail(productSetId);
		if(!result.isSuccess()){
			return new RestBody<List<ConfigurationVO>>(result.getCode(),result.getMessage());
		}
		return new RestBody<List<ConfigurationVO>>(SkuResultCode.SUCCESS,result.getDataModel());

	}

	/**
	 * 产品库对比
	 * @param productSetIds
	 * @return
	 */
	@RequestMapping(value = "productSetCompare", method = RequestMethod.GET)
	@ResponseBody
	public RestBody<Map<String, ProductSetCompareVO>> productSetCompare(String productSetIds) {
		String[] productSetIdStr = productSetIds.split(",");
		List<Integer> productSetIdList = new ArrayList<>();
		for (String str : productSetIdStr) {
			productSetIdList.add(Integer.valueOf(str));
		}

		Result<Map<String, ProductSetCompareVO>> result = productSetManager.productSetCompare(productSetIdList);
		if (!result.isSuccess()) {
			return new RestBody<Map<String, ProductSetCompareVO>>(result.getCode(), result.getMessage());
		}
		return new RestBody<Map<String, ProductSetCompareVO>>(SkuResultCode.SUCCESS, result.getDataModel());
	}
	
	/**
	 * 创建
	 * shenpingfeng
	 * 2017年1月4日
	 * @param productSetVO
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public RestBody<Integer> create(ProductSetVO productSetVO,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Integer userId = (Integer) session.getAttribute(WebConsts.USER_ID);
		productSetVO.setUserId(userId);

		List<ProductSetComponent> productSetComponents = JSON.parseArray(productSetVO.getProductList(), ProductSetComponent.class);
		if(productSetComponents == null || productSetComponents.size() == 0){
			return new RestBody<Integer>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		productSetVO.setProductSetComponentList(productSetComponents);

		Result<Integer> result = productSetManager.createProductSet(productSetVO);
		if(!result.isSuccess()){
			return new RestBody<Integer>(result.getCode(),result.getMessage());
		}
		return new RestBody<Integer>(SkuResultCode.SUCCESS,result.getDataModel());

	}
	/**
	 * 更新产品
	 * shenpingfeng
	 * 2017年1月5日
	 * @param productSetVO
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public RestBody<Integer> update(ProductSetVO productSetVO,HttpServletRequest request) {
		Result<Integer> result = productSetManager.updateProductSet(productSetVO);
		if(!result.isSuccess()){
			return new RestBody<Integer>(result.getCode(),result.getMessage());
		}
		return new RestBody<Integer>(SkuResultCode.SUCCESS,result.getDataModel());

	}
	
	/**
	 * 删除部品
	 * shenpingfeng
	 * 2017年1月9日
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteComponent", method = RequestMethod.GET)
	@ResponseBody
	public RestBody<Integer> deleteComponent(Integer id,HttpServletRequest request) {
		Result<Integer> result = productSetManager.deleteComponent(id);
		if(!result.isSuccess()){
			return new RestBody<Integer>(result.getCode(),result.getMessage());
		}
		return new RestBody<Integer>(SkuResultCode.SUCCESS);

	}

	/**
	 * 复制重命名产品库
	 *
	 * @param productSetId
	 * @param productSetName
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "productSetRename ", method = RequestMethod.GET)
	@ResponseBody
	public RestBody<Integer> productSetRename(Integer productSetId, String productSetName, HttpServletRequest request) {
		HttpSession session = request.getSession();
		Integer userId = (Integer) session.getAttribute(WebConsts.USER_ID);

		Result<Integer> result = productSetManager.renameProductSet(productSetId, productSetName, userId);
		if (!result.isSuccess()) {
			return new RestBody<Integer>(result.getCode(), result.getMessage());
		}
		return new RestBody<Integer>(SkuResultCode.SUCCESS);
	}
}
